package com.jt.provider.controller;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author CCFu
 * @version 1.0
 * @date 2022/1/19 17:21
 */
@Slf4j
@RefreshScope
@RestController
public class ProviderCacheController {
//    private static Logger log=
//            LoggerFactory.getLogger(ProviderCacheController.class);
    /**
     * 从配置文件读取配置赋值给属性
     */

    @Value("${useLocalCache:false}")
    private boolean useLocalCache;

    @GetMapping("/provider/cache01")
    public String doUseLocalCache(){
        return String.format("useLocalCache's value is %s" ,useLocalCache);
    }
        private CopyOnWriteArrayList<String> cache=new CopyOnWriteArrayList<>();
    @GetMapping("/provider/cache02")
    public List<String> doUseLocalCache02(){
        if(!useLocalCache){
           log.info("select data from database");
           return Arrays.asList("a","b","c");
        }
        if (cache.isEmpty()){
           List<String> data =  Arrays.asList("a","b","c");
           cache.addAll(data);
        }
        log.info("select data from cache");
        return cache;
    }
}
